package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.log.MachineAlarmLogDao;
import com.tbit.main.pojo.MachineAlarmLog;
import com.tbit.main.service.GeoreGionService;
import com.tbit.main.service.MachineAlarmLogService;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2020-11-17 10:30
 * 资产预警日志
 */
@Service("machineAlarmLogService")
public class MachineAlarmLogServiceImpl implements MachineAlarmLogService {
    @Autowired
    private MachineAlarmLogDao machineAlarmLogDao;
    @Autowired
    private GeoreGionService georeGionService;

    @Override
    public void insert(MachineAlarmLog machineAlarmLog) {
        machineAlarmLogDao.insert(machineAlarmLog);
    }

    @Override
    public void addBath(List<MachineAlarmLog> list) {
        machineAlarmLogDao.addBauth(list);
    }

    @Override
    public void insertBatch(Integer accountId, Integer alarmType, List<String> userCode, String dateTime) {
        List<List<String>> userCodes = ListUtils.partition(userCode, 900);
        for (List<String> code : userCodes) {
            machineAlarmLogDao.insertBatch(accountId, alarmType, code, dateTime);
        }
    }

    @Override
    public void insertBatchMap(Integer accountId, Integer alarmType, List<Map<String, Object>> userCodeRegionId, String dateTime) {
        List<List<Map<String, Object>>> userCodes = ListUtils.partition(userCodeRegionId, 900);
        for (List<Map<String, Object>> code : userCodes) {
            machineAlarmLogDao.insertBatchMap(accountId, alarmType, code, dateTime);
        }
    }

    @Override
    public List<MachineAlarmLog> getMachineLog(Integer accountId, Integer accountUserId, String startTime, String endTime, Integer alarmType, List<Integer> regionId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime + " 00:00:00");
        params.put(DaoParamConstant.endTime, endTime + " 59:59:59");
        params.put("alarmType", alarmType);
        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUserId);
        }
        params.put("regionId", StringUtil.getListString(regionId));

        return machineAlarmLogDao.getMachineLog(params);
    }

}
